package bblrtc

import (
	mux "github.com/julienschmidt/httprouter"
	"net"
	"net/http"
	"time"
)

type HttpServer struct {
	listener net.Listener
	router   *Router
	NodeId   string
}

func (hs *HttpServer) ListenAndServe() error {
	muxx := mux.New()
	muxx.POST("/rscp/register", hs.on_register)
	muxx.POST("/rscp/unregister", hs.on_unregister)
	muxx.POST("/rscp/login", hs.on_login)
	muxx.POST("/rscp/logout", hs.on_logout)
	// muxx.POST("/rscp/invite", hs.on_invite)
	// muxx.POST("/rscp/ack", hs.on_ack)
	muxx.POST("/rscp/offer", hs.on_offer)
	muxx.POST("/rscp/answer", hs.on_answer)
	muxx.POST("/rscp/candidate", hs.on_candidate)
	// muxx.POST("/rscp/busy", hs.on_busy)
	muxx.POST("/rscp/bye", hs.on_bye)
	muxx.POST("/rscp/wait", hs.on_wait)
	muxx.GET("/rscp/wait", hs.on_wait)

	muxx.NotFound = forbidden
	//router.PanicHandler = internal_error
	server := http.Server{
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 20,
		Handler:        muxx,
	}
	server.SetKeepAlivesEnabled(true)
	syslog.Println("Http Server Listen at " + hs.listener.Addr().String())
	return server.Serve(hs.listener)
}
